<?php

namespace july\julyadmin\builder\forms;

use july\julyadmin\builder\FormBase;

/**
 * 表单input email组件
 */
class Email extends FormBase
{
    /**
     * 添加组件
     * @Author
     * @DateTime 2023-07-16T12:14:26+0800
     * @param    string                   $field 字段
     * @param    string                   $label 名称
     * @return this
     */
    public function __construct($field = '', $label = '')
    {
        $form              = new \stdClass();
        $form->type        = 'email';
        $form->field       = $field;
        $form->label       = $label;
        $form->placeholder = $label;
        $form->help        = '';
        $form->tips        = ['tips' => '', 'direction' => 2, 'time' => 0, 'background' => '#000000'];
        $form->required    = false;
        $form->style       = '';
        //内容
        $form->value     = '';
        $form->default   = '';
        $form->maxlength = '';
        $this->form      = $form;
        return $this;
    }

    /**
     * 设置style
     * @Author
     * @DateTime 2023-07-16T12:19:05+0800
     * @param    string                   $style 样式，直接写css
     * @return   this
     */
    public function style($style = '')
    {
        $this->form->style = $style;
        return $this;
    }

    /**
     * 设置最多可以输入字符数
     * @Author
     * @DateTime 2023-07-16T12:38:40+0800
     * @param    integer                  $val 最多字符数量
     * @return   this
     */
    public function maxlength($val = 100)
    {
        $this->form->maxlength = $val;
        return $this;
    }

    /**
     * 渲染组件
     * @Author
     * @DateTime 2023-07-16T12:22:46+0800
     * @return   array
     */
    public function render($items_field = '')
    {
        if ($items_field) {
            $this->html        = '';
            $this->script      = '';
            $this->form->value = '';
        }
        $this->form->value = $this->form->default;
        if (isset($this->data[$this->form->field]) && $this->data[$this->form->field] !== "") {
            $this->form->value = $this->data[$this->form->field];
        }

        $required_html = $this->form->required && !$this->isView ? ' <font style="color:red;font-weight:bold;font-size:20px;vertical-align:-webkit-baseline-middle;">*</font>' : '';

        //处理tips
        $tips = '';
        if ($this->form->tips['tips']) {
            $tips = '<font class="form-tips-icon"><i class="mdi mdi-information-outline" tips="' . $this->form->tips['tips'] . '" direction="' . $this->form->tips['direction'] . '" time="' . $this->form->tips['time'] . '" background="' . $this->form->tips['background'] . '" onclick="showTips(this)"></i></font>';
        }

        $this->html .= <<< EOT

        <div class="row ps-0 pe-0 ms-0 me-0 mt-3 mb-3 col-md-{$this->colWidth}">
            <label class="col-sm-{$this->labelWidth} col-form-label pe-2 col-form-label-sm text-sm-end text-md-end text-lg-end text-xl-end">
                {$tips}{$this->form->label}
                {$required_html}
            </label>
EOT;
        $input_html = "";
        if (!$this->isView) {
            $input_html .= '<input type="email" class="form-control" id="form-' . $items_field . $this->form->field . $this->items_row_id_template_fun . '" name="' . $this->items_row_prefix . $this->form->field . '" placeholder="' . $this->form->placeholder . '" value="' . $this->form->value . '" ' . ($this->form->required ? ' required' : '') . ' style="' . $this->form->style . '" ' . $this->readonly . ($this->form->maxlength !== '' ? ' maxlength="' . $this->form->maxlength . '"' : '') . '/>';
        } else {
            $input_html .= '<font class="d-flex align-items-center h-100">';
            $input_html .= $this->form->value !== '' ? $this->form->value : '空';
            $input_html .= '</font>';
        }

        //处理tips
        $tips = '';
        if ($this->form->tips['tips']) {
            $tips = '<font class="form-tips-icon"><i class="mdi mdi-information-outline" tips="' . $this->form->tips['tips'] . '" direction="' . $this->form->tips['direction'] . '" time="' . $this->form->tips['time'] . '" background="' . $this->form->tips['background'] . '" onclick="showTips(this)"></i></font>';
        }
        $input_html .= $tips;

        //处理help
        if ($this->form->help && !$this->isView) {
            $input_html .= <<<EOT

            <div class="form-help form-text">
                <i class="mdi mdi-information-outline"></i>
                <font>{$this->form->help}</font>
            </div>
EOT;
        }

        $this->html .= <<< EOT

            <div class="col-sm-{$this->inputWidth}">
                {$input_html}
            </div>
        </div>
EOT;
        $result = ['html' => $this->html, 'script' => $this->script];
        return $result;
    }
}
